<?php
/**
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package Textbook
* @subpackage User
* @author Aljaz Srebrnic <a2piratesoft@gmail.com>;
* @author E. Pavletič
* @copyright 2009 a2PirateSoftware
* @license GPL License v3 <http://www.gnu.org/copyleft/gpl.html>
*/

require_once(ZNF::MODULES_DIR.'/Textbook/Form/AbstractTextbookForm.php');

class Textbook_Form_TextbookForm extends Textbook_Form_AbstractTextbookForm {

  private $id = null;
  private $isbn = null;
  private $title = null;
  private $authors = null;
  private $publisher = null;
  private $published = null;
  private $price = null;
  private $forfree = false;
  private $forsale = false;
  private $forchange = false;
  private $comment = null;

  public function populate() {
    if (isset($_POST['id']) && (strlen(trim($_POST['id'])) > 0))
      $this->id = intval($_POST['id']);

    if (isset($_POST['isbn']) && (strlen(trim($_POST['isbn'])) > 0))
      $this->isbn = $this->normalizeISBN($_POST['isbn']);

    if (isset($_POST['title']) && (strlen(trim($_POST['title'])) > 0))
      $this->title = htmlspecialchars(addslashes(trim($_POST['title'])));

    if (isset($_POST['authors']) && (strlen(trim($_POST['authors'])) > 0))
      $this->authors = htmlspecialchars(addslashes(trim($_POST['authors'])));

    if (isset($_POST['publisher']) && (strlen(trim($_POST['publisher'])) > 0))
      $this->publisher = htmlspecialchars(addslashes(trim($_POST['publisher'])));

    if (isset($_POST['published']) && (strlen(trim($_POST['published'])) > 0))
      $this->published = intval($_POST['published']);

    if (isset($_POST['price']) && (strlen(trim($_POST['price'])) > 0))
      $this->price = round(floatval(str_replace(',', '.', $_POST['price'])), 2); // Zamenja tudi morebitne vejice s piko

    if ((isset($_POST['freeOrSale'])) && !strcmp(trim($_POST['freeOrSale']), 'forfree'))
      $this->forfree = true;

    if ((isset($_POST['freeOrSale'])) && !strcmp(trim($_POST['freeOrSale']), 'forsale'))
      $this->forsale = true;

    if (isset($_POST['forchange']) && (strlen(trim($_POST['forchange'])) > 0))
      $this->forchange = true;

    if (isset($_POST['comment']) && (strlen(trim($_POST['comment'])) > 0))
      $this->comment = htmlspecialchars(addslashes(trim($_POST['comment'])));
  }


  public function validate() {
    $errors = new ZNF_Action_ActionErrors();
    $translation =  new ZNF_Presentation_Translation('Textbook', $_SESSION['znf']['lang']);

    if (($this->getTitle() == null) || strlen($this->getTitle()) > 60)
      $errors->addError(2, $translation->get('errorTitleNotValid'));

    if (($this->getAuthors() == null) || strlen($this->getAuthors()) > 200)
      $errors->addError(3, $translation->get('errorAuthorsNotValid'));

    if (($this->getPublisher() == null) || strlen($this->getPublisher()) > 60)
      $errors->addError(4, $translation->get('errorPublisherNotValid'));

    if (($this->getPublished() == null) || $this->getPublished() == 0)
      $errors->addError(5, $translation->get('errorPublishedNotValid'));

    if ((($this->getPrice() == 0) && $this->getForsale()) ||  // Prodaja brez cene (== 0)
	(($this->getPrice() != 0) && $this->getForfree()))    // Darilo s ceno (> 0)
      $errors->addError(6, $translation->get('errorPriceNotValid'));

    if (!($this->getForfree() || $this->getForsale() || $this->getForchange()) ||
	($this->getForfree() && $this->getForsale()))
      $errors->addError(7, $translation->get('errorFreeOrSaleNotValid'));

    return $errors;
  }

  public function getId() {
    return $this->id;
  }

  public function getIsbn() {
    return $this->isbn;
  }

  public function getTitle() {
    return $this->title;
  }

  public function getAuthors() {
    return $this->authors;
  }

  public function getPublisher() {
    return $this->publisher;
  }

  public function getPublished() {
    return $this->published;
  }

  public function getPrice() {
    return $this->price;
  }

  public function getForfree() {
    return $this->forfree;
  }

  public function getForsale() {
    return $this->forsale;
  }

  public function getForchange() {
    return $this->forchange;
  }

  public function getComment() {
    return $this->comment;
  }
}
?>
